package com.cction.fragment

import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.cction.R
import com.cction.comm.CctionPresenterImpl
import com.cction.comm.Utils
import com.cction.entry.CctionBean
import com.cction.entry.DetailBean
import com.zhy.adapter.recyclerview.CommonAdapter
import com.zhy.adapter.recyclerview.base.ViewHolder
import kotlinx.android.synthetic.main.detail_tab_fragment.*

class CctionDetailFragment : Fragment() {
    private var cctionBean: CctionBean? = null
    private val arrayList = ArrayList<DetailBean>()
    private val cctionPresenter = CctionPresenterImpl()


    /**
     * 静态方法
     */
    companion object {
        fun newInstance(cctionBean: CctionBean): CctionDetailFragment {
            val detailTabFragment = CctionDetailFragment()
            val bundle = Bundle()
            bundle.putSerializable("time", cctionBean)
            detailTabFragment.arguments = bundle
            return detailTabFragment
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.detail_tab_fragment, container, false)
    }


    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        initView()
    }


    private fun initView() {
        cctionBean = arguments?.getSerializable("time") as CctionBean

        when (cctionBean?.type) {
            1, 2 -> {
                when (cctionBean?.type) {
                    1 -> {
                        detail_title.text = "复利"
                        detail_title.setBackgroundResource(R.drawable.btn_round_bg_1)
                        detail_item_root.setBackgroundResource(R.color.orange)
                    }
                    2 -> {
                        detail_title.text = "本金"
                        detail_title.setBackgroundResource(R.drawable.btn_round_bg_2)
                        detail_item_root.setBackgroundResource(R.color.blue)
                    }
                }
                detail_principal_txt.text = "复利本金:"
                detail_principal_value.text = cctionBean?.principal


                detail_date_txt.text = "投资期限:"
                var currentDate = cctionBean?.date
                var currentInterestText = ""
                var currentDateTypeText = ""
                when (cctionBean?.operation_type) {
                    1 -> {
                        currentDate += "年"
                        currentInterestText = "年"
                        currentDateTypeText = "按年"
                    }
                    2 -> {
                        currentDate += "月"
                        currentInterestText = "月"
                        currentDateTypeText = "按月"
                    }
                    3 -> {
                        currentDate += "日"
                        currentInterestText = "日"
                        currentDateTypeText = "按日"
                    }
                }
                detail_date_value.text = currentDate
                currentInterestText += "利率："
                detail_interest_txt.text = currentInterestText
                detail_interest_value.text = "${Utils.keepPointString(Utils.stringToDouble(cctionBean?.interest))}%"
                detail_date_type_value.text = currentDateTypeText

                detail_interest_total.text = Utils.keepPointString(Utils.stringToDouble(cctionBean?.result) - Utils.stringToDouble(cctionBean?.principal))
                detail_total.text = cctionBean?.result
                detail_interest_total_txt.text = "复利利息:"
                detail_total_txt.text = "复利终值:"
            }
            3, 4 -> {

                detail_principal_txt.text = "每次定投:"
                detail_interest_total_txt.text = "定投利息:"
                detail_total_txt.text = "定投终值:"

                detail_date_txt.text = "投资期限:"
                var currentDate = cctionBean?.date
                var currentInterestText = ""
                var currentDateTypeText = ""
                when (cctionBean?.operation_type) {
                    1 -> {
                        currentDate += "年"
                        currentInterestText = "年"
                        currentDateTypeText = "按年"
                    }
                    2 -> {
                        currentDate += "月"
                        currentInterestText = "月"
                        currentDateTypeText = "按月"
                    }
                    3 -> {
                        currentDate += "日"
                        currentInterestText = "日"
                        currentDateTypeText = "按日"
                    }
                }
                detail_date_value.text = currentDate
                currentInterestText += "利率："
                detail_interest_txt.text = currentInterestText
                detail_interest_value.text = "${Utils.keepPointString(Utils.stringToDouble(cctionBean?.interest))}%"
                detail_date_type_value.text = currentDateTypeText

                when (cctionBean?.type) {
                    3 -> {
                        detail_title.text = "定投"
                        detail_title.setBackgroundResource(R.drawable.btn_round_bg_3)
                        detail_item_root.setBackgroundResource(R.color.green)
                        detail_total.text = cctionBean?.result
                        detail_principal_value.text = cctionBean?.principal
                        detail_interest_total.text = Utils.keepPointString(Utils.stringToDouble(cctionBean?.result) - Utils.stringToDouble(cctionBean?.principal) * Utils.stringToDouble(cctionBean?.date))
                    }
                    4 -> {
                        detail_title.text = "本利"
                        detail_title.setBackgroundResource(R.drawable.btn_round_bg_4)
                        detail_item_root.setBackgroundResource(R.color.lightgreen)
                        detail_principal_value.text = cctionBean?.result
                        detail_total.text = cctionBean?.principal
                        detail_interest_total.text = Utils.keepPointString(Utils.stringToDouble(cctionBean?.principal) - Utils.stringToDouble(cctionBean?.result) * Utils.stringToDouble(cctionBean?.date))
                    }
                }

            }
        }

        arrayList.clear()
        arrayList.addAll(cctionPresenter.detail(cctionBean))
        detail_recycler.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        detail_recycler.adapter = object : CommonAdapter<DetailBean>(context, R.layout.detail_item, arrayList) {
            override fun convert(holder: ViewHolder, detailBean: DetailBean, position: Int) {
                if (detailBean.postion % 2 == 0) {
                    holder.setBackgroundColor(R.id.detail_item_root, Color.parseColor("#eeeeee"))
                } else {
                    holder.setBackgroundColor(R.id.detail_item_root, Color.parseColor("#FFFFFF"))
                }
                holder.setText(R.id.detail_item_date, "${detailBean.postion}")
                holder.setText(R.id.detail_item_interest, "${detailBean.interest}%")
                holder.setText(R.id.detail_item_interest_total, "${detailBean.interest_total}")
                holder.setText(R.id.detail_item_total, detailBean.total)
            }
        }
    }
}
